import uuid
from collections import Callable

import pygame


class UIBase(pygame.sprite.LayeredUpdates):
    def __init__(self, rect, layer):
        super().__init__()
        import locals
        from constants import game
        self.uuid = uuid.uuid4().hex
        self.layer = layer
        self.rect = pygame.Rect(rect)
        self.action_mouse_button_down: [Callable] = None
        self.action_mouse_button_up: [Callable] = None
        self.action_mouse_move: [Callable] = None
        self.action_update: [Callable] = None
        locals.list_ui[self.layer].append(self)
        game.list_mouse_motion.append(self.ConditionCollide)

    def SetPosition(self, pos, position_type="topleft"):
        for sp in self.sprites():
            setattr(sp.rect, position_type, pos)

    def CorrectPosition(self):
        for sp in self.sprites():
            sp.rect.topleft = [self.rect[0] + sp.rect.x, self.rect[1] + sp.rect.y]

    def InitAction(self):
        from constants import game
        if self.action_mouse_move:
            game.list_mouse_motion.append(self.action_mouse_move)
        if self.action_mouse_button_down:
            game.list_mouse_button_down.append(self.action_mouse_button_down)
        if self.action_mouse_button_up:
            game.list_mouse_button_up.append(self.action_mouse_button_up)
        if self.action_update:
            game.list_update.append(self.action_update)

    def Destroy(self):
        self.DestroyAction()
        from constants import game
        import locals
        game.ui_group.remove(self)
        locals.list_ui[self.layer].remove(self)
        for sp in self.sprites():
            from Lib.Core.UISprite import UISprite
            if isinstance(sp, UISprite):
                sp.Destroy()

    def DestroyAction(self):
        from constants import game
        if self.action_mouse_move:
            game.list_mouse_motion.remove(self.action_mouse_move)
        if self.action_mouse_button_down:
            game.list_mouse_button_down.remove(self.action_mouse_button_down)
        if self.action_mouse_button_up:
            game.list_mouse_button_up.remove(self.action_mouse_button_up)
        if self.action_update:
            game.list_update.remove(self.action_update)
        game.list_mouse_motion.remove(self.ConditionCollide)
        from Lib import UtilsGame
        if UtilsGame.InAllUI(self): UtilsGame.RemoveUIInAll(self)
        import locals
        if locals.mouse_click_focus_ui == self:
            locals.mouse_click_focus_ui = None
        if locals.mouse_move_focus_ui == self:
            locals.mouse_click_focus_ui = None

    def Condition(self, pos):
        return self.rect.collidepoint(pos[0], pos[1])

    def ConditionCollide(self, pos):
        from locals import list_ui_collide
        if self.Condition(pos):
            if self not in list_ui_collide[self.layer]:
                list_ui_collide[self.layer].append(self)
        else:
            if self in list_ui_collide[self.layer]:
                list_ui_collide[self.layer].remove(self)
